<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods='goods'></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					// 查询关键词
					query: '',
					// 商品分类id
					cid: '',
					// 页码
					pageNum: 1,
					// 数据条数
					pageSize: 20
				},
				goodsList: [],
				total: 0,
				isLoading: false
			};
		},
		onLoad(options) {
			// 将页面传递参数转存在queryObj
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''

			this.getGoodsList()
		},
		methods: {
			// 获取商品列表数据的方法
			async getGoodsList(cb) {
				this.isLoading = true;
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
				this.isLoading = false;
				cb && cb();
				if (res.meta.status !== 200) return uni.$showMsg();
				this.goodsList = [...this.goodsList, ...res.message.goods];
				this.total = res.message.total;

			},
			onReachBottom() {
				// 判断数据结束
				if (this.queryObj.pageNum * this.queryObj.pageSize > this.total) return uni.$showMsg('没有更多数据了...');
				if (this.isLoading) return;
				this.queryObj.pageNum += 1;
				this.getGoodsList()
			},
			gotoDetail(goods) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}
		},
		onPullDownRefresh() {
			this.queryObj.pageNum = 1;
			this.total = 0;
			this.isLoading = false;
			this.goodsList = [];

			this.getGoodsList(() => uni.stopPullDownRefresh())
		}

	}
</script>

<style lang="scss">

</style>
